package com.study.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.services.DemantService;
import com.study.services.SolutionSercice;

@Controller
public class SolutionController {
	@Autowired
	SolutionSercice Ss;
	@Autowired
	DemantService ds;
	/**
	 * 加载新增界面
	 * Wcx
	 * 
	 * @param dname
	 */
	@GetMapping("addsolu")
	public ModelAndView addopp() {
		System.out.println("新建进来了");
		ModelAndView mv = new ModelAndView("beforeSale/solutionadd");
		mv.addObject("addsolution",  new JSONObject());
		return mv;
	}
	
	/**
	 * 展示页面
	 * @return
	 */
	@GetMapping("solution")
	public ModelAndView deptAdd() {
		// @GetMapping没有带斜杠/，默认的视图是jsp/add.jsp页面
		ModelAndView mv = new ModelAndView("beforeSale/solution");
		System.out.println("来了老弟");
		return mv;
	}
	
/**
 * 数据传输
 * @param theme
 * @param privoder
 * @param custname
 * @param opp
 * @param degree
 * @param content
 * @return
 */
	@PostMapping("addsolution")
	@ResponseBody
	public ModelAndView aa(String theme,String oppid,Date sdate,String cont,String fback,String custid,HttpServletRequest request) {
		System.out.println("我来了Post");
		System.out.println(oppid);
		String useid= request.getSession().getAttribute("useid").toString();
		boolean result = Ss.insertsolu( theme, oppid, sdate, cont, fback,custid,useid);
		ModelAndView mv = new ModelAndView("/beforeSale/solution");
		
		List<JSONObject> list = ds.selectopp(oppid);
		if (list.size()==0) {
			List<JSONObject> list1 = ds.selectopp1(oppid);
			if (list1.size()==0) {
				List<JSONObject> list2 = ds.selectopp2(oppid);
				
				if (list2.size()==0) {
					List<JSONObject> list3 = ds.selectopp3(oppid);
					if (list3.size()!=0) {
						ds.updatedem(oppid);
					}
					
				}else {
					ds.updatesolu(oppid);
				}
		
			}else {
				ds.updatecom(oppid);
			}
		}else {
			ds.updatedd(oppid);
		}
		return mv;
	}


	@PostMapping("updatesolution")
	@ResponseBody
	public ModelAndView bb(String theme,Date sdate,String cont,String fback,String custname,String solid,HttpServletRequest request) {
		
		String useid= request.getSession().getAttribute("useid").toString();
		System.out.println("我来了Post修改信息");
		System.out.println(theme+" "+ sdate+" "+cont+" "+ fback+" "+ solid);
		boolean result = Ss.updatedemand(custname, theme,sdate, cont, fback, solid,useid);
		ModelAndView mv = new ModelAndView("/beforeSale/demand");
		return mv;
	}
	/**
	 * 带条件的分页查询
	 * 
	 * @param input
	 * @param selects
	 * @param currentPage
	 * @param pagesize
	 * @param session
	 * @param request
	 * @return
	 */
	@PostMapping("beforeSale/TJsolution")
	@ResponseBody
	public String  getForm3(String input, int currentPage, int pagesize, HttpSession session,
			HttpServletRequest request) {
		Integer cusid = (Integer) session.getAttribute("cusview");
		System.out.println(input);
		String useid= request.getSession().getAttribute("useid").toString();
		List<JSONObject> list = Ss.selectBySel(currentPage, pagesize, input,useid,cusid);
		System.out.println(list.size() + "长度");
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
        System.out.println(json);
        session.removeAttribute("cusview");
		return json;
	}

	/**
	 * 修改数据回显
	 * @param linid
	 * @return
	 */
	@GetMapping("updatesolu")
	public ModelAndView updatelinkman(String id){
		System.out.println("进来了aa");
		System.out.println(id);
		ModelAndView mv = new ModelAndView("beforeSale/solutionadd");
		System.out.println("修改" +id +" 进来了");
		JSONObject jsonObject = Ss.selectByid(id);
		System.out.println(jsonObject);
		mv.addObject("addsolution",jsonObject);
		System.out.println("323");
		return mv;
	}
}
